//
//  GDWeatherController.m
//  GraduationDesign
//
//  Created by JiaKang.Zhong on 16/1/4.
//  Copyright © 2016年 JiaKang.Zhong. All rights reserved.
//

#import "GDWeatherController.h"
#import "GDWeatherID.h"
#import "GDNowWeatherModel.h"
#import "GDFutureWeatherModel.h"
#import "GDWeatherModelCenter.h"
#import "MBProgressHUD+MJ.h"
#import "GDTansitionModelDateOperation.h"
#import "GDFutureWeatherView.h"
#import "GDDownloadImageAndCachesOperation.h"

@interface GDWeatherController ()
/**
 *  接收天气模型组合
 */
@property(strong,nonatomic) GDWeatherModelCenter * weatherModelcCenter;

/**
 *  当前的天气模型
 */
@property(strong,nonatomic) GDNowWeatherModel * nowWeatherModel;

/**
 *  未来天气模型组合
 */
@property(strong,nonatomic) NSArray * futureWeatherModelArray;

/**
 *  未来天气模型
 */
@property(strong,nonatomic) GDFutureWeatherModel * futureWeatherModel;

@property (weak, nonatomic) IBOutlet UILabel *weather; // 天气状况
@property (weak, nonatomic) IBOutlet UILabel *drewwing_index;// 舒适程度
@property (weak, nonatomic) IBOutlet UILabel *temp;// 温度
@property (weak, nonatomic) IBOutlet UILabel *humidity;// 湿度
@property (weak, nonatomic) IBOutlet UILabel *wind_strength;// 风力大小
@property (weak, nonatomic) IBOutlet UILabel *time;// 更新时间
@property (weak, nonatomic) IBOutlet UILabel *dressing_advice;// 穿衣建议
@property (weak, nonatomic) IBOutlet UIImageView *fb;// 图标1
@property (weak, nonatomic) IBOutlet UIImageView *fa;//图标2
@property (weak, nonatomic) IBOutlet UILabel *wind_direction;//风向
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (nonatomic,strong) NSTimer * myTimer;
/**
 *  背景图片 url 存放容器
 */
@property(strong,nonatomic) NSArray  * mainBGArray;

@property(assign,nonatomic) int  img_index;

@property(strong,nonatomic) GDDownloadImageAndCaches *  downloadImage;
@end

@implementation GDWeatherController


-(GDDownloadImageAndCaches *)downloadImage{
    if (!_downloadImage) {
        _downloadImage = [[GDDownloadImageAndCaches alloc] init];
    }
    return _downloadImage;
}

-(NSArray *)mainBGArray{
    if (!_mainBGArray) {
        _mainBGArray = @[@"http://pic.yayabizhi.com:7704/uploads/131020/co131020112A6-0.jpg",
                         @"http://p3.so.qhimg.com/t012c8f5c91d2eb1c93.jpg"];
    }
    return _mainBGArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.img_index = -1;
//    加载背景图
    [self setWeatherMainBG];
}

-(void) setWeatherMainBG{
    /**
       存在一张默认的原始图片bg_night_sunny。后采用多线程不定期的去更改背景。url为网络资源，为事先存在的并放置在一个容器中
     */
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    NSString * imageFilePath = [[NSBundle mainBundle] pathForResource:@"bg_night_sunny.jpg" ofType:nil];
    imageView.image = [UIImage imageWithContentsOfFile:imageFilePath];
    [self.view addSubview:imageView];
    [self.view sendSubviewToBack:imageView];
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(downloadMainBG:) userInfo:imageView repeats:YES];
  
    
#pragma mark 创建返回主界面按钮
    

    
    UIButton * backToHomeItem = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
    [backToHomeItem setImage:[UIImage imageNamed:@"left-cross"] forState:UIControlStateNormal];
    [self.view addSubview:backToHomeItem];
    [backToHomeItem addTarget:self action:@selector(backToHomeClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void) backToHomeClick{
   [ self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidDisappear:(BOOL)animated{
    GDLog(@"取消计划任务");
    [super viewDidDisappear:animated];
    [self.myTimer invalidate];
    self.myTimer = nil;
}




#pragma mark 多线程下载更新背景  原理：SDWebimageCaches  但是由于占位图片比较大，整个屏幕，在切换的时候会有闪动，所以代码控制
-(void) downloadMainBG: (NSTimer *)timer{
    UIImageView *imageView = (UIImageView * )[timer userInfo];
    self.img_index = self.img_index == 1? 0:1;
//     判断用户是否已经下载过该图片  url作为唯一标示
    [self.downloadImage downloadImageAndCaches:imageView andUrl:self.mainBGArray[self.img_index]];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [MBProgressHUD showMessage:@"正在为您获取数据中.请稍等..." toView:self.view];
    
    /**  采用Block传值
     *  调用工具类中api。请求数据，获取数据成功并赋值给Model时，将Model返回给当前控制器。当控制器接受到之后
     *  更新UI界面
     *
     */
  [GDTansitionModelDateOperation initDateWithWeather:self success:^(GDWeatherModelCenter *model) {
      self.weatherModelcCenter = model;
      [MBProgressHUD hideHUDForView:self.view animated:YES];
      /**
       *  更新UI
       */
      [self updateUI];
      
  } failure:^(NSError *error) {
      if (error) {
           [MBProgressHUD hideHUDForView:self.view animated:YES];
          [MBProgressHUD showError:[NSString stringWithFormat:@"%@",error] toView:self.view];
      }
  }];


}


#pragma mark 更新ui
-(void) updateUI{
/*          当前天气的UI更新        */
    self.nowWeatherModel = self.weatherModelcCenter.nowWeatherModel;
    GDWeatherID * nowWeatherID = self.nowWeatherModel.weatherID;
    GDLog(@" 当前的天气状况 %s %@ 对应的天气图标 %@ %@",__func__,self.nowWeatherModel,nowWeatherID.fa,nowWeatherID.fb);
    self.weather.text = self.nowWeatherModel.weather;
     self.temp.text   = self.nowWeatherModel.temp;
     self.drewwing_index.text = self.nowWeatherModel.dressing_index;
     self.humidity.text = self.nowWeatherModel.humidity;
     self.wind_strength.text = self.nowWeatherModel.wind_strength;
     self.time.text = self.nowWeatherModel.time;
     self.dressing_advice.text = self.nowWeatherModel.dressing_advice;
     self.wind_direction.text = self.nowWeatherModel.wind_direction;
     self.fa.image = [UIImage imageNamed:nowWeatherID.fa];
     self.fb.image = [UIImage imageNamed:nowWeatherID.fb];
   
    
/*          未来天气的UI更新        */
    self.futureWeatherModelArray = self.weatherModelcCenter.futureWeatherModelArray;
    self.futureWeatherModel = self.futureWeatherModelArray[0];
    GDWeatherID * futureWeatherID = self.futureWeatherModel.weatherID_future;
    GDLog(@" 未来的天气状况 %s %ld  对应的天气图标 %@ %@",__func__,self.futureWeatherModelArray.count,futureWeatherID.fa,futureWeatherID.fb);
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bottomView.frame.size.width, self.bottomView.frame.size.height)];
    scrollView.contentSize = CGSizeMake((self.futureWeatherModelArray.count+1) * 100 + (self.futureWeatherModelArray.count + 1) * 5, 100);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;// 设置分页效果
    [self.bottomView addSubview:scrollView];
    for (int index = 0; index <self.futureWeatherModelArray.count; index ++) {
        GDFutureWeatherView * futureWeatherView = [[[NSBundle mainBundle] loadNibNamed:@"GDFutureWeatherView" owner:nil options:nil] lastObject];
        GDFutureWeatherModel * model = self.futureWeatherModelArray[index];
        futureWeatherView.futureModel = model;
        futureWeatherView.frame = CGRectMake( index * futureWeatherView.frame.size.width, 0, futureWeatherView.frame.size.width, futureWeatherView.frame.size.height);
        [scrollView addSubview:futureWeatherView];
    }

}

@end
